﻿<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <CustomAction Id="fd313b63-6d99-418e-82b1-00e8fefe41f0"
                Description="Activates the current item"
                RegistrationType="List"
                Location="CommandUI.Ribbon"
                Title="lamber.jcp.buttons"
                RegistrationId="30000">
    <CommandUIExtension>
      <CommandUIDefinitions>
        <CommandUIDefinition Location="Ribbon.Documents.Manage.Controls._children" >
          <Button Id="lamber.jcp.buttons.activateJCP" LabelText="Activate Script" TemplateAlias="o2" Image16by16="/_layouts/images/SolutionActivate_16x16.png" Image32by32="/_layouts/images/SolutionActivate_32x32.png" Sequence="1" Command="jcpActivateJCP"></Button>
        </CommandUIDefinition>
        <CommandUIDefinition Location="Ribbon.Documents.Manage.Controls._children">
          <Button Id="lamber.jcp.buttons.deactivateJCP" LabelText="Deactivate Script" TemplateAlias="o2" Image16by16="/_layouts/images/SolutionDeactivate_16x16.png" Image32by32="/_layouts/images/SolutionDeactivate_32x32.png" Sequence="2" Command="jcpDeactivateJCP"></Button>
        </CommandUIDefinition>
      </CommandUIDefinitions>
      <CommandUIHandlers>
        <CommandUIHandler Command="jcpActivateJCP" CommandAction="javascript: 
                          function onUpdateSuccess(sender, args) { SP.UI.Notify.addNotification('Executed. Please refresh page...'); }
                          
                          var currentContext = new SP.ClientContext.get_current(); 
                          var web = currentContext.get_web();
                          var currentListId = SP.ListOperation.Selection.getSelectedList();
                          var selectedItems = SP.ListOperation.Selection.getSelectedItems(currentContext);
                          
                          if (selectedItems.length > 0) {
                            var item = web.get_lists().getById(currentListId).getItemById(selectedItems[0].id);
                            currentContext.load(item);
                            item.set_item('jcp_activated', true);
                            item.update();   
                            currentContext.executeQueryAsync(onUpdateSuccess, onUpdateSuccess);                            
                          }
                          else {
                            SP.UI.Notify.addNotification('Operation failed');
                          }
                          "></CommandUIHandler>
        <CommandUIHandler Command="jcpDeactivateJCP" CommandAction="javascript:
                          function onUpdateSuccess(sender, args) { SP.UI.Notify.addNotification('Executed. Please refresh page...');   }
                          
                          var currentContext = new SP.ClientContext.get_current(); 
                          var web = currentContext.get_web();
                          var currentListId = SP.ListOperation.Selection.getSelectedList();
                          var selectedItems = SP.ListOperation.Selection.getSelectedItems(currentContext);
                          
                          if (selectedItems.length > 0) {
                            var item = web.get_lists().getById(currentListId).getItemById(selectedItems[0].id);
                            currentContext.load(item);
                            item.set_item('jcp_activated', false);
                            item.update();   
                            currentContext.executeQueryAsync(onUpdateSuccess, onUpdateSuccess);                            
                          }
                          else {
                            SP.UI.Notify.addNotification('Operation failed');
                          }">"</CommandUIHandler>
      </CommandUIHandlers>
    </CommandUIExtension>
  </CustomAction>
</Elements>